import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";
import MainVue from '@/view/MainVue';
import HomeVue from '@/view/HomeVue';
import AuditMessage from '@/view/AuditMessage'
import DataInfo from '@/view/DataInfo'
import GroupChat from '@/view/GroupChat'
import NoticeInfo from '@/view/NoticeInfo'
import PayInfo from '@/view/PayInfo'
import PersonalInfo from '@/view/PersonalInfo'
import StoreInfo from '@/view/StoreInfo'
import UserLogin from '@/view/user/UserLogin'
import UserRegister from '@/view/user/UserRegister'
import SystemUser from '@/view/system/SystemUser'
import SystemRole from '@/view/system/SystemRole'
import AuditContent from '@/view/AuditContent'

const routes = [
        {
            path: '/',
            component: MainVue,
            meta:{requestAuth:true},
            redirect:'/home',
            children: [
                { path: '/home', name: 'home', component: HomeVue, meta: { requestAuth: true } },
                { path: '/data', name: 'data', component: DataInfo, meta: { requestAuth: true } },
                { path: '/store', name: 'store', component: StoreInfo, meta: { requestAuth: true } },
                { path: '/notice', name: 'notice', component: NoticeInfo, meta: { requestAuth: true } },
                { path: '/personal', name: 'personal', component: PersonalInfo, meta: { requestAuth: true } },
                { path: '/pay', name: 'pay', component: PayInfo, meta: { requestAuth: true } },
                { path: '/chat', name: 'chat', component: GroupChat, meta: { requestAuth: true } },
                { path: '/system/role', name: 'role', component: SystemRole, meta: { requestAuth: true } },
                { path: '/system/user', name: 'user', component: SystemUser, meta: { requestAuth: true } },
                { 
                    path: '/audit', 
                    component: AuditMessage, 
                    meta: { requestAuth: true },
                    children: [
                        {
                            path:':id',
                            component:AuditContent,
                           
                        }
                    ]
                },
            ]
        },
        {
            path: '/login',
            name: "login",
            component: UserLogin,
            meta:{requestAuth:false}
        },
        {
            path: '/register',
            name: "register",
            component: UserRegister,
            meta:{requestAuth:false}
        },
    ]
const router = createRouter({
    history:createWebHistory(),
    routes
})
// 如果没有登录就不能访问其他页面
router.beforeEach((to,from,next) => {
    if(!store.state.user.isLogin && to.meta.requestAuth){
        next({name:"login"});
    }else{
        next();
    }
})

export default router